PROJECT=KegScaleMk2
SOURCEBOOST=${HOME}/wine/SourceBoost

ifeq (${SIMULATOR}, 1)
PROCESSOR=PIC18F1320
else
PROCESSOR=PIC18F14K50
endif

ifeq ($(findstring PIC18,PIC18F14K50), PIC18)
    SERIES = 18
else
    SERIES = 16
endif
HEX=../hex
ifeq (${SIMULATOR}, 1)
  OBJ=../obj/sim
  PROJECT := ${PROJECT}_sim
  C_OPTS  := -d SIMULATOR
  DEPFILE := Serial_deps.mak
else
  OBJ=../obj
  LD_OPTS := -rb 0x1000
  DEPFILE := Dependencies.mak
endif

PIC_LD=wine ${SOURCEBOOST}/boostlink.pic.exe -t ${PROCESSOR} -ld ${SOURCEBOOST}/Lib/ libc.pic${SERIES}.lib eeprom.pic${SERIES}.lib ${LD_OPTS}

PIC_CC=wine ${SOURCEBOOST}/boostc.pic${SERIES}.exe -t ${PROCESSOR} -i -obj ${OBJ} -I./pic_pack -I. ${C_OPTS}

PIC_PP=wine ${SOURCEBOOST}/pp.exe -I./pic_pack -I. -I${SOURCEBOOST}/include -m -d _${PROCESSOR} -d _PIC${SERIES} -d_CHAR_INDEX -d_BOOSTC

all: dummy ${DEPFILE} ${HEX}/${PROJECT}_boost.hex ${HEX}/${PROJECT}_gpasm.hex

dummy:
	mkdir -p ${OBJ} 2> /dev/null || true
	mkdir ${HEX} 2> /dev/null || true
	mkdir ${DEP} 2> /dev/null || true

C_FILES:= kegscale_mk2.c \
         utils.c \
         display.c \
         button.c \
         io_interface.c \
         eeprom_utils.c \
         eeprom_init.c \
         pic_pack/pic_utils.c \
         pic_pack/pic_serial.c \
         pic_pack/lcd.c

ifneq (${SIMULATOR}, 1)
  C_FILES:= ${C_FILES} \
         usb_config_serial.c \
         pic_pack/usb_cdc_class.c \
         pic_pack/pic_usb_buffer_mgt.c \
         pic_pack/pic_usb.c
endif

ifneq ($(wildcard ${DEPFILE}), ${DEPFILE})
    foo:= $(shell perl build_dep.pl "$(PIC_PP)" ${DEPFILE} ${C_FILES})
endif
include ${DEPFILE}

dep: 
	perl build_dep.pl "$(PIC_PP)" ${DEPFILE} ${C_FILES}

${DEPFILE}: Makefile build_dep.pl
	perl build_dep.pl "$(PIC_PP)" ${DEPFILE} ${C_FILES}
	@echo
	@echo "###################################"
	@echo "Rebuilt dependencies.  Rerun 'make'"
	@echo "###################################"
	@false
	

${HEX}/${PROJECT}_boost.hex: ${OBJ_FILES}
	$(PIC_LD) -p ${PROJECT}_boost -d ../hex $^

${HEX}/${PROJECT}_gpasm.hex: ${HEX}/${PROJECT}_boost.hex
	gpasm -p ${PROCESSOR} -o ${HEX}/${PROJECT}_gpasm.hex ${HEX}/${PROJECT}_boost.asm

eeprom_init.c: eeprom_init.cfg compile_eeprom.pl
	perl compile_eeprom.pl > eeprom_init.c

usb_serial: ${HEX}/usb_serial.hex
	@echo

${HEX}/usb_serial.hex: ${OBJ}/pic_utils.obj ${OBJ}/pic_serial.obj ${OBJ}/usb_cdc_class.obj \
                       ${OBJ}/pic_usb_buffer_mgt.obj ${OBJ}/pic_usb.obj ${OBJ}/usb_config_serial.obj \
                       ${OBJ}/usb_serial.obj
	$(PIC_LD) -p usb_serial -d ../hex $^

${OBJ}/usb_serial.obj: usb_serial.c config.h
	${PIC_CC} $<

